package main;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;

import fluxo.Cliente;

@SuppressWarnings("serial")
public class JFMain extends JFrame {

	private static JFMain frame;
	private JPanel contentPane;
	private JPanel fieldPane;
	private JPanel buttonPane;
	private JScrollPane sPaneHistorico;
	private JScrollPane sPaneMensagem;
	private JTextPane pHistorico;
	private JTextPane pMensagem;
	private JButton btnEnviar;
	private String IP = "127.0.0.1";
	private String nome = "Claudemir";
	private Thread t;
	private Socket socket;

	/**
	 * Launch the application.
	 */

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new JFMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws IOException
	 * @throws UnknownHostException
	 */

	public JFMain() throws UnknownHostException, IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setTitle("Trabalho IV");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		fieldPane = new JPanel(new GridBagLayout());
		contentPane.add(fieldPane, BorderLayout.CENTER);

		pHistorico = new JTextPane();
		pHistorico.setEditable(false);

		sPaneHistorico = new JScrollPane(pHistorico);
		sPaneHistorico
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		fieldPane.add(sPaneHistorico, new GridBagConstraints(0, 0, 1, 5, 1.0,
				10.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(5, 5, 5, 5), 0, 0));

		pMensagem = new JTextPane();
		
		InputMap input = pMensagem.getInputMap();
		KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
		KeyStroke shiftEnter = KeyStroke.getKeyStroke("shift ENTER");
		input.put(shiftEnter, "INSERT_BREAK");
		input.put(enter, "TEXT_SUBMIT");
		
		ActionMap actions = pMensagem.getActionMap();
		actions.put("TEXT_SUBMIT", new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				btnEnviar.doClick();
			}
		});
		
		actions.put("INSERT_BREAK", new AbstractAction() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				pMensagem.setText(pMensagem.getText() + "\n");
			}
		});

		sPaneMensagem = new JScrollPane(pMensagem);
		fieldPane.add(sPaneMensagem, new GridBagConstraints(0, 6, 1, 2, 1.0,
				1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(5, 5, 5, 5), 0, 0));

		socket = new Socket(IP, 5000);
		t = new Cliente(socket, nome, pHistorico);
		t.start();

		buttonPane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		btnEnviar = new JButton("Enviar");
		btnEnviar.setContentAreaFilled(false);
		btnEnviar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				((Cliente) t).sendMessage(pMensagem.getText());
				pMensagem.setText("");
			}
		});
		buttonPane.add(btnEnviar);
	}
}
